﻿@page "/fetchdata"
@using CatTalkWeb_Blazor_Ver_.Shared
@inject HttpClient Http

<PageTitle>Weather forecast</PageTitle>

<h1>Weather forecast</h1>

<p>This component demonstrates fetching data from the server.</p>

@if (forecasts == null) {
    <p><em>Loading...</em></p>
} else {
    <table class="table">
        <thead>
            <tr>
                <th>Date</th>
                <th>Temp. (C)</th>
                <th>Temp. (F)</th>
                <th>Summary</th>
            </tr>
        </thead>
        <tbody>
            @foreach (var forecast in forecasts) {
                <tr>
                    <td>@forecast.Date.ToShortDateString()</td>
                    <td>@forecast.TemperatureC</td>
                    <td>@forecast.TemperatureF</td>
                    <td>@forecast.Summary</td>
                </tr>
            }
        </tbody>
    </table>
}
<AntDesign.Charts.Line @ref="@chart1" Data="chartData" Config="config" />
@code {
    IChartComponent chart1;
    private WeatherForecast[]? forecasts;
    Object[]? chartData;
    object[] data1 =
        {
        new {date = "2019-01-01", height = "123"},
        new {date = "2019-02-01", height = "123"},
        new {date = "2019-03-01", height = 3.5},
        new {date = "2019-04-01", height = 5},
        new {Date = "2019-05-01", height = 4.9, festival = "劳动节"},
        new {date = "2019-06-01", height = 6},
        new {date = "2019-07-01", height = 7},
        new {date = "2019-08-01", height = 9},
        new {date = "2019-09-01", height = 3},
        new {date = "2019-10-01", height = 13, festival = "国庆节"},
        new {date = "2019-11-01", height = 6},
        new {date = "2019-12-01", height = 23}
    };
    protected override async Task OnInitializedAsync() {
        forecasts = await Http.GetFromJsonAsync<WeatherForecast[]>(@"http://localhost:5011/api/WeatherForecast/Get");
        chartData = new Object[forecasts.Length];
        for (int i = 0; i < forecasts.Length; i++) {
            chartData[i] = new { date = forecasts[i].Date.ToString(), temperature = forecasts[i].TemperatureC };
        }
        await chart1.ChangeData(chartData);
        StateHasChanged();
        await base.OnInitializedAsync();
    }

    LineConfig? config = new LineConfig() {
            Title = new AntDesign.Charts.Title() {
                Text = "天气",
                Visible = true
            },
            Description = new Description() {
                Visible = true,
                Text = "近几日的喵星天气数据（随机瞎编写的）"
            },
            ForceFit = true,
            Padding = "auto",
            XField = "date",
            YField = "temperature",
            Point = new LineViewConfigPoint {
                Visible = true
            },
            YAxis = new ValueAxis {
                Nice = true
            },
            Label = new Label {
                Visible = false
            }
        };
}

